

/**
 * 创建entity PolylineVolume
 */
import Entity from '../Entity'

const DEFAULT_STYLE = {
  clampToGround: true
}

export default class PolylineVolume extends Entity {

  /**
   * 创建一个Polyline实体对象
   * @param {*} positions
   * @param {*} options 
   */
  constructor(positions, options = {}) {
    super()
    this._positions = positions
    this._type = 'polylineVolume'
    this._radius = options.radius

    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }

    this.delegate = new Cesium.Entity({ polylineVolume: {} })
    this.mounted()
  }

  /**
  * 初始化对象属性
  */

  mounted() {
    this.positions = this._positions
    this.radius = this._radius
    this.style = this._style
  }

  /** 
  * @param {*} style Circle的样式
  * @returns 
  */

  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    Object.assign(this.delegate.polylineVolume, { ...this._style })
  }

  /**
   * 计算半径
   * @param {*} radius 
   * @returns {array} 
   */
  computedCircel(radius){
    let positions = []
    for (var i = 0; i < 360; i++) {
      var radians = Cesium.Math.toRadians(i);
      positions.push(
        new Cesium.Cartesian2(
          radius * Math.cos(radians),
          radius * Math.sin(radians)
        )
      )
    }
    return positions
  }

  get radius(){
    return this._radius
  }

  set radius(val){
    this._radius = val
    this.delegate.polylineVolume.shape = this.computedCircel(this._radius)
  }

  get style() {
    return this._style
  }

  set style(val) {
    Object.assign(this._style, { ...val })
    Object.assign(this.delegate.polylineVolume, { ...this._style })
  }

  get positions() {
    return this._positions
  }

  set positions(val) {
    this._positions = val
    this.delegate.polylineVolume.positions = this._positions
  }
}
